package com.gxa.service.impl;

import com.gxa.exception.CustomerException;
import com.gxa.mapper.ManagerMapper;
import com.gxa.pojo.Manager;
import com.gxa.service.ManagerService;

import com.gxa.util.Md5Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 伍炳清
 * @date 2020-09-21 18:52
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private ManagerMapper managerMapper;

    @Override
    //添加管理员
    public void addManager(Manager manager) throws CustomerException {

        //判断用户、用户名、秘密是否为空
        if (manager == null) {
            throw new CustomerException("用户为为空!");
        }
        if (manager.getUsername() == null || manager.getUsername().trim().equals("")) {
            throw new CustomerException("用户名不能为空!");
        }
        if (manager.getPassword() == null || manager.getPassword().trim().equals("")) {
            throw new CustomerException("密码不能为空为空!");
        }
        //判断用户名是否重复
        Manager manager1 = managerMapper.findByUserName(manager.getUsername());
        if (manager1 != null) {
            throw new CustomerException("用户名已存在!");
        }
//        生成盐
        String salt = Md5Utils.createSalt();
        //密码加密
        String md5Password = Md5Utils.md5Password(manager.getPassword(), salt);

        //存盐、存加密秘密
        manager.setPassword(md5Password);
        manager.setSalt(salt);
        //成功添加管理员
        managerMapper.insert(manager);

    }

    @Override
//    管理员登录
    public Manager login(String username, String password) throws CustomerException {
        // 1.判空
        if (username == null || username.trim().equals("")) {
            throw new CustomerException("用户名不能为空!");
        }
        if (password == null || password.trim().equals("")) {
            throw new CustomerException("密码不能为空!");
        }
        //查询用户的盐
        Manager manager = managerMapper.findByUserName(username);
        if (manager == null) {
            throw new CustomerException("用户名或密码不正确!");
        }

        //加密登录密码
        String md5Password = Md5Utils.md5Password(password, manager.getSalt());

        //对比加密后的密码
        if (!manager.getPassword().equals(md5Password)) {

            throw new CustomerException("用户名或密码不正确!");
        }


        return manager;
    }

    @Override
    //管理员修改密码
    public void updatePassword(String username, String oldPassword, String newPassword) throws CustomerException {
        // 1.判空
        if (username == null || username.trim().equals("")) {
            throw new CustomerException("用户名不能为空!");
        }
        if (oldPassword == null || oldPassword.trim().equals("")) {
            throw new CustomerException("旧密码不能为空!");
        }
        if (newPassword == null || newPassword.trim().equals("")) {
            throw new CustomerException("新密码不能为空!");
        }
        //查询用户的盐
        Manager manager = managerMapper.findByUserName(username);
        if (manager == null) {
            throw new CustomerException("用户名或密码不正确!");
        }
        //加密旧登录密码
        String md5Password = Md5Utils.md5Password(oldPassword, manager.getSalt());

        //对比加密后的密码
        if (!manager.getPassword().equals(md5Password)) {
            throw new CustomerException("用户名或密码不正确!");
        }

        //生成新的盐
        String newsalt = Md5Utils.createSalt();

        //加密新密码
        String newMd5Password = Md5Utils.md5Password(newPassword, newsalt);
        //存储新密码、新盐
        manager.setPassword(newMd5Password);
        manager.setSalt(newsalt);

        //完成修改操作
        managerMapper.updatePassword(manager.getUsername(),
                md5Password,
                newMd5Password,
                newsalt);


    }

    @Override
    //修改管理员基本信息
    public void updateManager(Manager manager) throws CustomerException {

        //判断用户名是否重复
        Manager manager1 = managerMapper.findByUserName(manager.getUsername());
        if (manager1 != null && !manager1.getId().equals(manager.getId())) {
            throw new CustomerException("用户名已存在!");
        }

        managerMapper.updateById(manager);

    }

    @Override
    //通过id查询管理员
    public Manager findManagerById(Integer id) throws CustomerException {
        if (id == null) {

            throw new CustomerException("id不能为空！");
        }

        Manager manager = managerMapper.findById(id);

        if (manager == null) {
            throw new CustomerException("用户不存在");
        }

        return manager;

    }

    @Override
//    删除管理员
    public void deleteManager(Integer id) throws CustomerException {
        if (id == null) {
            throw new CustomerException("id不能为空！");
        }
        Manager manager = managerMapper.findById(id);
        if (manager == null) {
            throw new CustomerException("需要删除的管理员不存在!");
        }

        managerMapper.deleteById(id);

    }


}
